﻿<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<CarHelpDesk.Web.Models.Answer>>" %>
<div id="idx">
    <h3>Отговори</h3>
    <%  
        ViewDataDictionary viewData = new ViewDataDictionary();
        bool isOwner, canMark = false;
        if (Model.Count() == 0 ||! Request.IsAuthenticated)
            isOwner = false;
        else
            isOwner = Model.First().Question.AuthorID == (Guid)System.Web.Security.Membership.GetUser().ProviderUserKey;// (Guid)System.Web.Security.Membership.GetUser().ProviderUserKey;
        if (Model.Count(answer => answer.IsCorrect) == 0 && isOwner)
        {
            viewData["canMark"] = true;
            canMark = true;
        }
        foreach (var item in Model)
       {
           Html.RenderPartial("SingleAnswer", item,viewData);
       } %>
</div>

<script type="text/javascript">
    function markAnswer(aid) {
        $.post("/Answers/MarkAsAccepted/", { answerID: aid }, onMarkAnswerComplete, "json");
    }

    function onMarkAnswerComplete(data) {
        if (data.errorCode != 0)
            $("#accept-answer" + data.answerID).feedback(data.message, { duration: 2000, right: true, type: "error" });
        else
        {
            $("#accept-answer" + data.message).attr("class", "accept-answer-icon accept-answer-yes");
            $("#answerImg" + data.message).attr("src", "../../Content/icons/acceptedAnswer.png");
        }
    }

    $().ready(function() {

        if (<%=canMark.ToString().ToLower()%>)
        {            
            $('div.answer > div > center > span.accept-answer-icon').attr('class', 'accept-answer-icon accept-answer-no');
        }
        else
        {           
            $('div.answer > div > center > span.accept-answer-icon').css('cursor', 'default');
            $('div.answer > div > center > span.accept-answer-icon').attr('class', 'accept-answer-icon accept-answer-empty');
            $('div.answer > div > center > span.accept-answer-icon').attr('onclick', '');
        }
        $("div.answerAccepted > div > center > span.accept-answer-icon").attr("class", "accept-answer-icon accept-answer-yes");

    });
    
</script>
